import cv2
import os
from tqdm import tqdm

# 图片文件夹路径和输出视频文件路径
image_folder = "/home/cxz/Pictures/隧道缺陷数据//data/images/train"
output_video = "/home/cxz/analyze/samples/video/全部20fps.mp4"

# 获取图片文件夹中的所有图片文件名，并按照文件名排序
images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]
images.sort()

# 获取第一张图片的宽度和高度
img_path = os.path.join(image_folder, images[0])
img = cv2.imread(img_path)
height, width, layers = img.shape

# 创建视频编写器对象
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
# 第三个参数是fps，可以是小数
video = cv2.VideoWriter(output_video, fourcc, 20, (1920, 1080))

# 逐个将图片写入视频
for image in tqdm(images):
    img_path = os.path.join(image_folder, image)
    img = cv2.imread(img_path)
    img = cv2.resize(img, (1920, 1080), interpolation=cv2.INTER_LINEAR)

    video.write(img)

# 释放视频编写器对象
video.release()
